﻿#-------------------------------------------------------------------------------
# Name:        module1
# Purpose:
# Author:      doucube
#-------------------------------------------------------------------------------
#!/usr/bin/env python
#coding=utf-8

import os, io, sys, re, time
import winreg, json, random, subprocess
import webbrowser, urllib.request, urllib.parse
import lxml.html

#keyword = "abc"

#正则：用来过滤出页面正文搜索结果列表
s_re_content = '<table cellpadding="0" cellspacing="0" class="result".+?</table>'
#正则：用来过滤出页面正文搜索结果的标题
s_re_content_title = '<font size="3">.+?</a>'
#正则：用来过滤出页面正文搜索结果的摘要
s_re_content_abstract = '<font size="?-1"?>.+?<br/?>'
#正则：用来过滤出页面正文搜索结果的URL信息
s_re_content_url = '<font color="#008000">.+?</font>'


def verifySearch(bhtml, keyword):
    #use Xpath to verify the title is baidu search
    root = lxml.html.document_fromstring(bhtml)
    if root.xpath('''//title''')[0].text.find("百度搜索") != -1 \
       and root.xpath('''//title''')[0].text.find(keyword) != -1:
        print(root.xpath('''//title''')[0].text)
        return True
    else:
        #print("获取页面失败: " + root.xpath('''//title''')[0].text)
        return False

def doucube():
    #use Xpath to verify the title is baidu search
    print("doucube 开始--------------------------------")
    print("doucube 开始--------------------------------")
    print("doucube 开始--------------------------------")
    print("doucube 开始--------------------------------")

def vefifyPing():
    print("网络连通性测试开始--------------------------------")
    p = subprocess.Popen(["ping.exe", 'www.baidu.com'],
                                         stdin = subprocess.PIPE,
                                         stdout = subprocess.PIPE,
                                         stderr = subprocess.PIPE,
                                         shell = True)
    out = p.stdout.read().decode()
    #if out.find("TTL") != -1:
    if out.find("0% loss") != -1:
        print("网络连通性：成功--------------------------------")
        return True
    else:
        print("网络连通性：失败--------------------------------")
        return False

def verifyAuth():
    return True

class authorize():
    def __init__(self):
        self.
    
class BaiDu():
    def __init__( self, keyword, pagenumber):
        self.keyword = keyword
        self.keyword_encode = keyword.encode("utf-8")
        self.pn = 0 if pagenumber == 0 else (pagenumber - 1) * 10

    def request(self):
        #construct the search command, example: ie=utf-8&wd=%E6%9D%A8%E5%B9%82&pn=0
        params = urllib.parse.urlencode({'wd': self.keyword_encode, 'pn': self.pn, "ie":"utf-8"})
        opener = urllib.request.build_opener()
        #add agent for simulate the browser, firefox
        opener.addheaders = [('User-agent', "Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20100101 Firefox/14.0.1)")]
        response = opener.open("http://www.baidu.com/s?" + params)
        #get the response baidu result html
        bhtml = response.read().decode("utf-8")
        return bhtml


def main():
    doucube()
    vefifyPing()
    keyword = "杨幂"
    iSearch = BaiDu(keyword, 1)
    iSearchResult = iSearch.request()
    if verifySearch(iSearchResult, keyword):
        print("OK")
    else:
        print("获取页面失败，可能已经被百度屏蔽，请休息下！")
        return False




if __name__ == '__main__':
    main()
